package com.invengo.testrfid.app.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.invengo.testrfid.MyApplication;
import com.invengo.testrfid.R;
import com.invengo.testrfid.app.dao.QueryManager;
import com.invengo.testrfid.app.dao.RfidCageBind;
import com.invengo.testrfid.app.utils.MyLogUtils;

public class ShowTagView extends LinearLayout {


    private TextView textTid;
    private TextView textEpc;
    private TextView textMessage;

    public ShowTagView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View inflate = LayoutInflater.from(context).inflate(R.layout.view_show_tag, this);
        textTid = inflate.findViewById(R.id.tv_tid);
        textEpc = inflate.findViewById(R.id.tv_epc);
        textMessage = inflate.findViewById(R.id.tv_message);
    }

    /**
     * 初始化界面
     */
    public boolean initView(String tid, String epc, String cageId) {

        textTid.setText(tid);
        textEpc.setText(epc);

        QueryManager queryManager = QueryManager.getManager();

        if (tid.length() == 0) {
            textMessage.setText("扣动扳机，初始化标签");
            return false;
        }

        RfidCageBind rfidCageBind = queryManager.getRfidCageQuery().queryDataByTid(tid);
        if (rfidCageBind != null) {
            textTid.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red));
            textMessage.setText("该标签已绑定，请更换其他标签！");
            return false;
        } else {
            textTid.setTextColor(MyApplication.getContext().getResources().getColor(R.color.text_l_g));
        }


        RfidCageBind epcBind = queryManager.getRfidCageQuery().queryDataByCageNo(epc);
        if (epcBind != null) {
            textEpc.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red));
            textMessage.setText("该标签的存储区已写入其他的集装箱编号！请更换其他标签！");
            return false;
        } else {
            textEpc.setTextColor(MyApplication.getContext().getResources().getColor(R.color.text_l_g));
        }


        if (!epc.trim().equals(cageId.trim())) {
            textEpc.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red));
            textMessage.setText("扣动扳机初始化该标签！");
            return false;
        } else {
            textEpc.setTextColor(MyApplication.getContext().getResources().getColor(R.color.text_l_g));
            textMessage.setText("当前标签已初始化完成！");
            return true;
        }
    }


    public boolean initCheckView(String tid, String epc, String cageId) {

        textTid.setText(tid);
        textEpc.setText(epc);

        if (epc.trim().equals(cageId.trim())) {

            textMessage.setText("标签写入的数据和集装箱编号一致！");
            textMessage.setTextColor(MyApplication.getContext().getResources().getColor(R.color.text_gray));
            return true;
        } else {

            textMessage.setText("标签写入的数据和集装箱编号不一致！\n数据库存储的集装箱编号为：\n" + cageId);
            textMessage.setTextColor(MyApplication.getContext().getResources().getColor(R.color.red));

            if (cageId.trim().length() == 0) {
                textMessage.setText("该标签未和集装箱绑定！");
                return true;
            }
            return false;
        }
    }


    public void initMessage(String message) {

        textMessage.setText(message);
    }
}
